/*
 *  Copyright 2010 UPF Telecos 4th group G.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package edu.upf.content;

import edu.upf.utils.Definitions;
import java.awt.Image;
import java.io.File;
import java.io.Serializable;
import javax.swing.ImageIcon;


/**
 * This class models an image with an id and a list of keywords
 * @author netjois
 */
public class ImageObject implements Serializable, Definitions, DescriptionContent{

    private String name;
    private String size;
    private String path;
    private String owner;
    private String format;
    private String popularity;
    private ImageIcon thumbnail;
    private String album;

    @Override
    public void setName(String name) {
        this.name = name;
        if(thumbnail!=null){
            thumbnail.setDescription(name);
        }
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setSize(String s) {
        size = s;
    }

    @Override
    public String getSize() {
        return size;
    }

    @Override
    public String getPath() {
        path="Shared_Folder/"+getAlbum()+"/"+getName()+"."+getFormat();
        return path;
    }

    @Override
    public void setPopularity(String p) {
        popularity = p;
    }

    @Override
    public String getPopularity() {
        return popularity;
    }

    @Override
    public void setOwner(String owner) {
        this.owner = owner;
    }

    @Override
    public String getOwner() {
        return owner;
    }
    
    @Override
    public void setFormat(String format){
        this.format=format;
    }

    @Override
    public String getFormat(){
        return format;
    }

    @Override
    public ImageIcon getThumbnail(){
        return thumbnail;
    }

    /**
     * Creates and stores a thumbnail with object information such as path, name
     * and extension
     * @param height
     * @param width
     */
    public void createThumbnail(int height, int width) {
        String uri = getPath();   
        if ((new File(uri).exists())) {
            int originalHeight, originalWidth;
            double resizeRatioH, resizeRatioW, resizeRatio;
            ImageIcon originalImage = new ImageIcon(uri);
            originalHeight = originalImage.getIconHeight();
            originalWidth = originalImage.getIconWidth();
            resizeRatioH = ((double) height / (double) originalHeight);
            resizeRatioW = ((double) width / (double) originalWidth);
            if (resizeRatioH > resizeRatioW) {
                resizeRatio = resizeRatioW;
            } else {
                resizeRatio = resizeRatioH;
            }
            height = (int) (originalHeight * resizeRatio);
            width = (int) (originalWidth * resizeRatio);
            Image img = originalImage.getImage();
            Image scaledImg = img.getScaledInstance(width, height,
                    java.awt.Image.SCALE_SMOOTH);
            thumbnail = new ImageIcon(scaledImg);
        } else {
            thumbnail = new ImageIcon(getClass().getResource("/thumb_icons/broken_link.png"));
        }
        thumbnail.setDescription(name);
    }

    @Override
    public String getAlbum() {
        return album;
    }

    @Override
    public void setAlbum(String album) {
        this.album=album;
    }
}

